using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qWDECK = from m in mtemp.WDECK select m;
       DBMapper.MapWDECK(qWDECK.ToList(), true);
       */
        public List<WDECK> MapWDECK(XElement item2, bool mergedb)
        {
            List<WDECK> result = new List<WDECK>();
            var item4 = from item3 in item2.Elements()
                        select new WDECK
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            WDMANDANT = (string)item3.Element("WDMANDANT"),
                            WDSA = (string)item3.Element("WDSA"),
                            WDAUFKRS = (string)item3.Element("WDAUFKRS"),
                            WDAUFLFD = (string)item3.Element("WDAUFLFD"),
                            WDAUFPOS = (string)item3.Element("WDAUFPOS"),
                            WDTYP = (string)item3.Element("WDTYP"),
                            WDUPOS = (string)item3.Element("WDUPOS"),
                            WDWAKZ = (string)item3.Element("WDWAKZ"),
                            WDART = (string)item3.Element("WDART"),
                            WDDESS = (string)item3.Element("WDDESS"),
                            WDCOLORIT = (string)item3.Element("WDCOLORIT"),
                            WDFARBE = (string)item3.Element("WDFARBE"),
                            WDBESCHICH = (string)item3.Element("WDBESCHICH"),
                            WDVAR = (string)item3.Element("WDVAR"),
                            WDDAT = (string)item3.Element("WDDAT"),
                            WDLO = (string)item3.Element("WDLO"),
                            WDBEDARF = (string)item3.Element("WDBEDARF"),
                            WDZUGET = (string)item3.Element("WDZUGET"),
                            WDRESERV = (string)item3.Element("WDRESERV"),
                            WDPRART = (string)item3.Element("WDPRART"),
                            WDPRVAR = (string)item3.Element("WDPRVAR"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapWDECK(result, mergedb);
          return result;
        }
        ///DBMapper.MapWDECK(mtemp.WDECK.ToList<WDECK>(), true);        
       public void MapWDECK(List<WDECK> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from WDECK row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.WDECK.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.WDECK.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.WDECK.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "WDECK")]
    public class WDECK : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _WDMANDANT="0";
        [Column]
        public string WDMANDANT
        {
            get
            {
                return _WDMANDANT;
            }
            set
            {
                if (value != _WDMANDANT)
                {
                     _WDMANDANT = value;
                    NotifyPropertyChanged("WDMANDANT");
                }
            }
        }
        private string  _WDSA="0";
        [Column]
        public string WDSA
        {
            get
            {
                return _WDSA;
            }
            set
            {
                if (value != _WDSA)
                {
                     _WDSA = value;
                    NotifyPropertyChanged("WDSA");
                }
            }
        }
        private string  _WDAUFKRS="0";
        [Column]
        public string WDAUFKRS
        {
            get
            {
                return _WDAUFKRS;
            }
            set
            {
                if (value != _WDAUFKRS)
                {
                     _WDAUFKRS = value;
                    NotifyPropertyChanged("WDAUFKRS");
                }
            }
        }
        private string  _WDAUFLFD="0";
        [Column]
        public string WDAUFLFD
        {
            get
            {
                return _WDAUFLFD;
            }
            set
            {
                if (value != _WDAUFLFD)
                {
                     _WDAUFLFD = value;
                    NotifyPropertyChanged("WDAUFLFD");
                }
            }
        }
        private string  _WDAUFPOS="0";
        [Column]
        public string WDAUFPOS
        {
            get
            {
                return _WDAUFPOS;
            }
            set
            {
                if (value != _WDAUFPOS)
                {
                     _WDAUFPOS = value;
                    NotifyPropertyChanged("WDAUFPOS");
                }
            }
        }
        private string  _WDTYP="0";
        [Column]
        public string WDTYP
        {
            get
            {
                return _WDTYP;
            }
            set
            {
                if (value != _WDTYP)
                {
                     _WDTYP = value;
                    NotifyPropertyChanged("WDTYP");
                }
            }
        }
        private string  _WDUPOS="0";
        [Column]
        public string WDUPOS
        {
            get
            {
                return _WDUPOS;
            }
            set
            {
                if (value != _WDUPOS)
                {
                     _WDUPOS = value;
                    NotifyPropertyChanged("WDUPOS");
                }
            }
        }
        private string  _WDWAKZ="0";
        [Column]
        public string WDWAKZ
        {
            get
            {
                return _WDWAKZ;
            }
            set
            {
                if (value != _WDWAKZ)
                {
                     _WDWAKZ = value;
                    NotifyPropertyChanged("WDWAKZ");
                }
            }
        }
        private string  _WDART="0";
        [Column]
        public string WDART
        {
            get
            {
                return _WDART;
            }
            set
            {
                if (value != _WDART)
                {
                     _WDART = value;
                    NotifyPropertyChanged("WDART");
                }
            }
        }
        private string  _WDDESS="0";
        [Column]
        public string WDDESS
        {
            get
            {
                return _WDDESS;
            }
            set
            {
                if (value != _WDDESS)
                {
                     _WDDESS = value;
                    NotifyPropertyChanged("WDDESS");
                }
            }
        }
        private string  _WDCOLORIT="0";
        [Column]
        public string WDCOLORIT
        {
            get
            {
                return _WDCOLORIT;
            }
            set
            {
                if (value != _WDCOLORIT)
                {
                     _WDCOLORIT = value;
                    NotifyPropertyChanged("WDCOLORIT");
                }
            }
        }
        private string  _WDFARBE="0";
        [Column]
        public string WDFARBE
        {
            get
            {
                return _WDFARBE;
            }
            set
            {
                if (value != _WDFARBE)
                {
                     _WDFARBE = value;
                    NotifyPropertyChanged("WDFARBE");
                }
            }
        }
        private string  _WDBESCHICH="0";
        [Column]
        public string WDBESCHICH
        {
            get
            {
                return _WDBESCHICH;
            }
            set
            {
                if (value != _WDBESCHICH)
                {
                     _WDBESCHICH = value;
                    NotifyPropertyChanged("WDBESCHICH");
                }
            }
        }
        private string  _WDVAR="0";
        [Column]
        public string WDVAR
        {
            get
            {
                return _WDVAR;
            }
            set
            {
                if (value != _WDVAR)
                {
                     _WDVAR = value;
                    NotifyPropertyChanged("WDVAR");
                }
            }
        }
        private string  _WDDAT="0";
        [Column]
        public string WDDAT
        {
            get
            {
                return _WDDAT;
            }
            set
            {
                if (value != _WDDAT)
                {
                     _WDDAT = value;
                    NotifyPropertyChanged("WDDAT");
                }
            }
        }
        private string  _WDLO="0";
        [Column]
        public string WDLO
        {
            get
            {
                return _WDLO;
            }
            set
            {
                if (value != _WDLO)
                {
                     _WDLO = value;
                    NotifyPropertyChanged("WDLO");
                }
            }
        }
        private string  _WDBEDARF="0";
        [Column]
        public string WDBEDARF
        {
            get
            {
                return _WDBEDARF;
            }
            set
            {
                if (value != _WDBEDARF)
                {
                     _WDBEDARF = value;
                    NotifyPropertyChanged("WDBEDARF");
                }
            }
        }
        private string  _WDZUGET="0";
        [Column]
        public string WDZUGET
        {
            get
            {
                return _WDZUGET;
            }
            set
            {
                if (value != _WDZUGET)
                {
                     _WDZUGET = value;
                    NotifyPropertyChanged("WDZUGET");
                }
            }
        }
        private string  _WDRESERV="0";
        [Column]
        public string WDRESERV
        {
            get
            {
                return _WDRESERV;
            }
            set
            {
                if (value != _WDRESERV)
                {
                     _WDRESERV = value;
                    NotifyPropertyChanged("WDRESERV");
                }
            }
        }
        private string  _WDPRART="0";
        [Column]
        public string WDPRART
        {
            get
            {
                return _WDPRART;
            }
            set
            {
                if (value != _WDPRART)
                {
                     _WDPRART = value;
                    NotifyPropertyChanged("WDPRART");
                }
            }
        }
        private string  _WDPRVAR="0";
        [Column]
        public string WDPRVAR
        {
            get
            {
                return _WDPRVAR;
            }
            set
            {
                if (value != _WDPRVAR)
                {
                     _WDPRVAR = value;
                    NotifyPropertyChanged("WDPRVAR");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<WDECK> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\WDECK\\WDECK" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\WDECK" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("WDECK");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (WDECK row in ListInput)
                {
                    XElement f = new XElement("WDECK");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement WDMANDANT = new XElement("WDMANDANT");
                         WDMANDANT.Value = row.WDMANDANT;
                          f.Add(WDMANDANT);
                        XElement WDSA = new XElement("WDSA");
                         WDSA.Value = row.WDSA;
                          f.Add(WDSA);
                        XElement WDAUFKRS = new XElement("WDAUFKRS");
                         WDAUFKRS.Value = row.WDAUFKRS;
                          f.Add(WDAUFKRS);
                        XElement WDAUFLFD = new XElement("WDAUFLFD");
                         WDAUFLFD.Value = row.WDAUFLFD;
                          f.Add(WDAUFLFD);
                        XElement WDAUFPOS = new XElement("WDAUFPOS");
                         WDAUFPOS.Value = row.WDAUFPOS;
                          f.Add(WDAUFPOS);
                        XElement WDTYP = new XElement("WDTYP");
                         WDTYP.Value = row.WDTYP;
                          f.Add(WDTYP);
                        XElement WDUPOS = new XElement("WDUPOS");
                         WDUPOS.Value = row.WDUPOS;
                          f.Add(WDUPOS);
                        XElement WDWAKZ = new XElement("WDWAKZ");
                         WDWAKZ.Value = row.WDWAKZ;
                          f.Add(WDWAKZ);
                        XElement WDART = new XElement("WDART");
                         WDART.Value = row.WDART;
                          f.Add(WDART);
                        XElement WDDESS = new XElement("WDDESS");
                         WDDESS.Value = row.WDDESS;
                          f.Add(WDDESS);
                        XElement WDCOLORIT = new XElement("WDCOLORIT");
                         WDCOLORIT.Value = row.WDCOLORIT;
                          f.Add(WDCOLORIT);
                        XElement WDFARBE = new XElement("WDFARBE");
                         WDFARBE.Value = row.WDFARBE;
                          f.Add(WDFARBE);
                        XElement WDBESCHICH = new XElement("WDBESCHICH");
                         WDBESCHICH.Value = row.WDBESCHICH;
                          f.Add(WDBESCHICH);
                        XElement WDVAR = new XElement("WDVAR");
                         WDVAR.Value = row.WDVAR;
                          f.Add(WDVAR);
                        XElement WDDAT = new XElement("WDDAT");
                         WDDAT.Value = row.WDDAT;
                          f.Add(WDDAT);
                        XElement WDLO = new XElement("WDLO");
                         WDLO.Value = row.WDLO;
                          f.Add(WDLO);
                        XElement WDBEDARF = new XElement("WDBEDARF");
                         WDBEDARF.Value = row.WDBEDARF;
                          f.Add(WDBEDARF);
                        XElement WDZUGET = new XElement("WDZUGET");
                         WDZUGET.Value = row.WDZUGET;
                          f.Add(WDZUGET);
                        XElement WDRESERV = new XElement("WDRESERV");
                         WDRESERV.Value = row.WDRESERV;
                          f.Add(WDRESERV);
                        XElement WDPRART = new XElement("WDPRART");
                         WDPRART.Value = row.WDPRART;
                          f.Add(WDPRART);
                        XElement WDPRVAR = new XElement("WDPRVAR");
                         WDPRVAR.Value = row.WDPRVAR;
                          f.Add(WDPRVAR);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
